package com.dhcc.bpm.modules.scf.service.HomePageService.HomePageImpl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.segmentId.LocalCacheIdGenerator;
import com.dhcc.bpm.modules.scf.dto.HomePageDTO.ScfAddressInfoDTO;
import com.dhcc.bpm.modules.scf.entity.HomePage.ScfAddressInfo;
import com.dhcc.bpm.modules.scf.mapper.HomePageMapper.ScfAddressInfoMapper;
import com.dhcc.bpm.modules.scf.service.HomePageService.IScfAddressInfoService;
import com.dhcc.bpm.modules.scf.service.IScfFileAttachmentInfoService;
import com.dhcc.bpm.modules.scf.vo.HomePageVO.ScfAddressInfoVO;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务实现类
 *
 * @author liuguoting
 * @since 2023-10-07
 */
@Service
public class ScfAddressInfoServiceImpl extends ServiceImpl<ScfAddressInfoMapper, ScfAddressInfo> implements IScfAddressInfoService {
    @Autowired
    private IScfFileAttachmentInfoService scfFileAttachmentInfoService;
    @Autowired
    private LocalCacheIdGenerator localCacheIdGenerator;

    @Override
    public IPage<ScfAddressInfoVO> selectScfAddressInfoPage(IPage<ScfAddressInfoVO> page, ScfAddressInfoDTO scfAddressInfo) {
        return page.setRecords(baseMapper.selectScfAddressInfoPage(page, scfAddressInfo));
    }

    @Override
    public Boolean addAddressInfo(String excelRowId) throws IOException {
        String filePath = scfFileAttachmentInfoService.getFilePath(excelRowId,null);
        List<ScfAddressInfo> scfAddressInfoList = new ArrayList<>(); // 创建一个集合用于存储数据
        EasyExcel.read(filePath, ScfAddressInfo.class, new AnalysisEventListener<ScfAddressInfo>() {
            @Override
            public void invoke(ScfAddressInfo scfAddressInfos, AnalysisContext analysisContext) {
                // 在这里处理每一行Excel数据
                scfAddressInfoList.add(scfAddressInfos); // 将数据添加到集合中
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            }
        }).sheet().doRead();

        for (ScfAddressInfo scfAddressInfoExcel : scfAddressInfoList) {

            // 开始补充对象字段start
            // 设置记录编号
            // 生成ROW_ID
            // 生成时间
            SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
            String formTime = timeFormat.format(new Date());
            String rowId = "AD" + localCacheIdGenerator.nextId() + formTime;
            scfAddressInfoExcel.setRowId(rowId);
            // 补充字段结束
            save(scfAddressInfoExcel);
        }
        // 拿到list后发送给mapper层存入数据库
        return R.success("数据插入完成！").isSuccess();
    }

    @Override
    public String selectAdcodeByAdname(String adname) {
        return baseMapper.selectAdcodeByAdname(adname);
    }

}
